//
//  IndexBtn.m
//  项目_02
//
//  Created by dllo on 15/10/6.
//  Copyright (c) 2015年 L-G. All rights reserved.
//

#import "Whatever.h"

#define kButtonHeight 30 
#define kButtonWidth 80
// 按钮间距
#define kSpacing 15
#define kBtnTitleColor [UIColor redColor]
#define kBtnTitleUnselected [UIColor blackColor]
#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375
@interface Whatever ()

// 按钮的标识条

@property (nonatomic, strong) UIView *lineView;

@end
@implementation Whatever

- (instancetype)initWithFrame:(CGRect)frame BtnTitleArray:(NSArray *)array
{
    self = [super initWithFrame:frame];
    if (self) {
        self.btnTitleArray = [NSArray arrayWithArray:array];
    }
    return self;
}

// 当数组传值进入时， 创建视图
- (void)setBtnTitleArray:(NSArray *)btnTitleArray
{
    if (_btnTitleArray != btnTitleArray) {
        _btnTitleArray = btnTitleArray;
    }
    UIScrollView *btnScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 2 * HEIGHT)];
    btnScrollView.tag = 1000;
    // 设置ScrollView的contentsize
    btnScrollView.contentSize = CGSizeMake((75 * WIDTH + 20 * WIDTH) * _btnTitleArray.count, 0);
    btnScrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    for (int i = 0; i < _btnTitleArray.count; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(10 * WIDTH + (75 * WIDTH + 20 * WIDTH) * i, 10 * HEIGHT, 75 * WIDTH, 30 * HEIGHT);
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        btn.titleLabel.lineBreakMode = 6;
        btn.tag = i + 1;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [btnScrollView addSubview:btn];
        
        [btn setTitle:_btnTitleArray[i] forState:UIControlStateNormal];
        
        if (i == 0) {
            [btn setTitleColor:kBtnTitleColor forState:UIControlStateNormal];
        }else
        {
            [btn setTitleColor:kBtnTitleUnselected forState:UIControlStateNormal];
        }
    }
    btnScrollView.showsHorizontalScrollIndicator = NO;
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(10 * WIDTH, 10 * HEIGHT + 30 * HEIGHT + 3 * HEIGHT, 75 * WIDTH, 2 * HEIGHT)];
    
    self.lineView.backgroundColor = [UIColor redColor];
    
    [btnScrollView addSubview:self.lineView];
    
    [self addSubview:btnScrollView];
}

- (void)btnAction:(UIButton *)sender
{
    for (UIButton *btn in [self viewWithTag:1000].subviews) {
        if([btn isKindOfClass:[UIButton class]]){
            if (btn.tag == sender.tag) {
                [btn setTitleColor:kBtnTitleColor forState:UIControlStateNormal];
            }else
            {
                [btn setTitleColor:kBtnTitleUnselected forState:UIControlStateNormal];
            }
        }
    }
    [UIView animateWithDuration:0.1 animations:^{
        self.lineView.frame = CGRectMake(sender.frame.origin.x, self.lineView.frame.origin.y, 75 * HEIGHT, 2);
    }];
    [self.delegate buttonOfWhateverClicked:sender];
}
- (void)setBtntag:(NSInteger)btntag
{
    UIScrollView *scroll = (UIScrollView *)[self viewWithTag:1000];
    CGFloat h = (((WIDTH * 120) * 7) - self.frame.size.width) * (btntag - 1) / (7 - 1);
    [scroll setContentOffset:CGPointMake(h, 0) animated:YES];
    UIButton *button = (UIButton *)[self viewWithTag:btntag];
    [self btnAction:button];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
